package com.itheima.article.consumer;

import com.itheima.article.service.ApArticleService;
import com.itheima.common.constants.BusinessConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class ApConsumer {
    //注入service层的对象
    @Autowired
    private ApArticleService apArticleService;

    @KafkaListener(topics = BusinessConstants.MqConstants.WM_NEWS_DOWN_OR_UP_TOPIC)
    public void apConsumer(ConsumerRecord<String , String> msg){
        log.info("开始消费信息，同步上下架 t={},msg={}",msg.topic(),msg.value());
        //判断是否同步上下架成功，打印日志
        if (!apArticleService.apConsumer(msg.value())){
            log.info("同步上下架失败 t={},msg={}",msg.topic(),msg.value());
        }else {
            log.info("消费信息完毕，同步上下架成功 t={},msg={}", msg.topic(), msg.value());
        }
    }
}
